define(function (require, exports, module)
{
  var $ = require('jquery');
  require('json2');
  var jxbaseinfo = require('jxbaseinfo');
  var jx_progressbar = require('jx_progressbar');
  var jx_msgbox = require('jx_msgbox');
  var $upload =
          {
            defaults: {
              site: jxbaseinfo.path,
              url: '',
              formid: jxbaseinfo.formid
            },
            msgbox: jx_msgbox,

            submitFile: function (config, okCallBack, cancelCallBack)
            {
              config = $.extend({}, this.defaults, config);
              jx_progressbar.show({time: 0, value: 1});
              $upload.msgbox.loadLayer(2);
              if ((undefined != config.url) && (config.url != ''))
                $("#" + config.formid).attr("action", config.url);

              $("#" + config.formid).attr("method", "post");
              $("#" + config.formid).attr("enctype", "multipart/form-data");
              $("#" + config.formid).attr("target", "upload_iframe");
              if ($("#upload_iframe").length == 0)
              {
                $.ajax({
                  async: false,
                  url: config.site + 'common/kit/upload_iframe.html', //这里是静态页的地址
                  type: "GET", //静态页用get方法，否则服务器会抛出405错误
                  success: function (data) {
                    $(document.body).append(data);
                    $("#upload_iframe").hide() ;
                  }
                });
              }
              $("#" + config.formid).submit();

              this.waitTime = setInterval(function ()
              {
                $.get(config.site + "common/uploadProgress", function (data)
                {
                  if (data.hasOwnProperty("progress"))
                  {
                    jx_progressbar.next(data.progress, data.progress + "%");

                    if (data.curstatus <= -100)
                    {
                      clearInterval($upload.waitTime);
                      $upload.msgbox.close();
                      jx_progressbar.hide();
                      $upload.msgbox.alert({content: data.message}, okCallBack);
                      return;
                    }
                  } else
                  {
                    clearInterval($upload.waitTime);
                    $upload.msgbox.close();
                    jx_progressbar.hide();
                    $upload.msgbox.alert({content: data.message}, okCallBack);
                    return;
                  }
                });

                if (undefined != $("#upload_iframe").contents().find("body"))
                {
                  var data = $.trim($("#upload_iframe").contents().find("body").html());
                  if ((data != ""))
                  {
                    if (undefined != $(data).find("#messageCode"))
                      if (null != $(data).find("#messageCode"))
                        $upload.msgbox.messageCode = $(data).find("#messageCode").text();

                    if (undefined != $(data).find("#messageType"))
                      if (null != $(data).find("#messageType"))
                        $upload.msgbox.messageType = parseInt($(data).find("#messageType").text());

                    if (undefined != $(data).find("#reserveMessage"))
                      if (null != $(data).find("#reserveMessage"))
                        $upload.msgbox.reserveMessage = $(data).find("#reserveMessage").text();

                    clearInterval($upload.waitTime);
                    $upload.msgbox.close();
                    jx_progressbar.hide();
                    if ($upload.msgbox.messageType != 3)
                      $upload.msgbox.alert({content: $("#upload_iframe").contents().find("html").html()}, okCallBack);
                    else
                      $upload.msgbox.confirm({content: $("#upload_iframe").contents().find("html").html()}, okCallBack, cancelCallBack);
                  }
                }

              }, 300);
            }
          };


  return $upload;
});